package com.d3works.apps.stock.db.repo;

import com.d3works.apps.base.db.Product;
import com.d3works.apps.stock.db.StockLocationLine;
import com.d3works.apps.stock.service.WeightedAveragePriceService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import javax.persistence.PersistenceException;

public class StockLocationLineStockRepository extends StockLocationLineRepository {

  @Override
  public StockLocationLine save(StockLocationLine entity) {
    try {
      Product product = entity.getProduct();
      if (entity.getIsAvgPriceChanged()) {
        Beans.get(WeightedAveragePriceService.class).computeAvgPriceForProduct(product);
      }
      return super.save(entity);
    } catch (Exception e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
      throw new PersistenceException(e.getMessage(), e);
    }
  }
}
